package com.gtw.modules.api.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.google.gson.Gson;
import com.gtw.common.annotation.LoginUserApi;
import com.gtw.common.utils.Query;
import com.gtw.common.utils.R;
import com.gtw.dto.TbCollectionDto;
import com.gtw.modules.api.service.TbCollectionService;
import com.gtw.modules.api.service.TbCommodityService;
import com.gtw.modules.dao.TbCollectionDao;
import com.gtw.modules.entity.TbCollection;
import com.gtw.modules.entity.TbUser;
import com.gtw.utils.EntityDtoConverter;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户收藏
 * Created by wuye on 2018/2/1.
 */
@RestController
@RequestMapping("/api/collection")
public class TbCollectionController {

    @Autowired
    private TbCollectionService tbCollectionService;
    @Autowired
    private TbCollectionDao tbCollectionDao;
    @Autowired
    private TbCommodityService tbCommodityService;

    /**
     * 查询列表
     *
     * @param params
     * @returnapi
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params, @LoginUserApi TbUser tbUser) {
        //查询列表数据
        Query query = new Query(params);
        query.put("uid", tbUser.getUuid());
        Page<TbCollection> pageUtil = new Page<TbCollection>(query.getPage(), query.getLimit());
        Page<TbCollection> page = tbCollectionService.queryPageList(pageUtil, query);
        List<TbCollectionDto> tbCollectionDtoList = new ArrayList<>();
        for(int i=0;i<page.getRecords().size();i++){
            TbCollectionDto tbCollectionDto = new TbCollectionDto();
            EntityDtoConverter edc = new EntityDtoConverter();
            try {
                tbCollectionDto = (TbCollectionDto) edc.entityConvertDto(page.getRecords().get(i),tbCollectionDto);
                tbCollectionDto.setTbCommodity(tbCommodityService.queryObject(page.getRecords().get(i).getCommodidyid()));
                tbCollectionDtoList.add(tbCollectionDto);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        Page<TbCollectionDto> tbCollectionDtoPage = new Page<TbCollectionDto>();
        tbCollectionDtoPage.setRecords(tbCollectionDtoList);
        return R.ok().put("data", tbCollectionDtoPage);
    }


    /**
     * 添加收藏
     *
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public R save(Integer commodityid, @LoginUserApi TbUser tbUser) {
        if (tbCollectionService.queryByCommodity(tbUser.getUuid(), commodityid) == null) {
            TbCollection tbCollection = new TbCollection();
            tbCollection.setUid(tbUser.getUuid());
            tbCollection.setCreatedate(new Date());
            tbCollection.setCommodidyid(commodityid);
            tbCollection.setIsdelete(false);
            tbCollection.setCommodityname(tbCommodityService.queryObject(commodityid).getCommodityname());
            tbCollection = tbCollectionService.save(tbCollection);
            return R.ok().put("data",tbCollection.getId());
        }else{
            return R.error("已经收藏过了～");
        }

    }

    /**
     * 查询是否收藏指定商品
     *
     * @return
     */
    @RequestMapping("/coll")
    public R coll(@LoginUserApi TbUser tbUser, Integer commodityid) {
        TbCollection tbCollection = tbCollectionService.queryByCommodity(tbUser.getUuid(), commodityid);
        if (tbCollection == null) {
            return R.ok().put("data", false);
        } else {
            return R.ok().put("data", tbCollection.getId());
        }
    }

    /**
     * 修改收藏
     *
     * @param tbCollection
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody TbCollection tbCollection) {
        tbCollectionService.update(tbCollection);
        return R.ok();
    }

    /**
     * 删除收藏
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public R delete(@RequestBody Integer[] ids) {
        tbCollectionService.deleteBatch(ids);
        return R.ok().put("data",false);
    }
}
